#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

void dfs(std::map<std::string, std::vector<std::string>> &tree,
         std::vector<std::string> &child_vec, std::string query_node)
{
    for (auto it = tree[query_node].begin(); it != tree[query_node].end(); it++)
    {
        child_vec.push_back(*it);
        dfs(tree, child_vec, *it);
    }
}

int main()
{
    int N;
    std::cin >> N;
    std::map<std::string, std::vector<std::string>> tree;
    for (int i = 0; i < N; i++)
    {
        std::string child, parent;
        std::cin >> child >> parent;
        tree[parent].push_back(child);
    }

    std::string query_node;
    std::cin >> query_node;
    std::vector<std::string> child_vec;
    dfs(tree, child_vec, query_node);

    std::sort(child_vec.begin(), child_vec.end());
    for (int i = 0; i < child_vec.size(); i++)
    {
        std::cout << child_vec[i] << std::endl;
    }

    return 0;
}